package com.dtw.ditouwang.ui.home;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.List;

public class ImagePeopleAdapter extends RecyclerView.Adapter<ImagePeopleAdapter.Holder> {
    private List<String> imgList;
    private OnItemClickListener onItemClickListener;

    CropSquareTransformation cropSquareTransformation = new CropSquareTransformation();
    RequestOptions options = RequestOptions.bitmapTransform(cropSquareTransformation);
    private RecyclerView recyclerView;

    public ImagePeopleAdapter(List<String> imgList) {
        this.imgList = imgList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_square_imageview, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        Glide.with(holder.imageView).load(WebApi.BaseUrl + imgList.get(i).replace("\\", "/")).apply(options).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        if(imgList==null){
            return 0;
        }
        return imgList.size();
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        super.onAttachedToRecyclerView(recyclerView);
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_image);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(recyclerView.getId(),getAdapterPosition(),0);
                    }
                }
            });
        }
    }
}
